package com.kmxd.ams.app.system;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmxd.ams.client.system.ISysCropSvc;
import com.kmxd.ams.client.system.ISysNoticeCompanySvc;
import com.kmxd.ams.client.system.dto.SysNoticeCompanyAddDTO;
import com.kmxd.ams.client.system.dto.SysNoticeCompanyEditDTO;
import com.kmxd.ams.client.system.dto.SysNoticeCompanyQueryDTO;
import com.kmxd.ams.client.system.vo.SysNoticeCompanyListVO;
import com.kmxd.ams.client.system.vo.SysNoticeCompanyVO;
import com.kmxd.ams.core.common.query.QueryGen;
import com.kmxd.ams.core.util.IdUtil;
import com.kmxd.ams.infra.system.entity.SysCrop;
import com.kmxd.ams.infra.system.entity.SysNoticeCompany;
import com.kmxd.ams.infra.system.mapper.SysNoticeCompanyMapper;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 通知查看范围 服务实现类
 *
 * @author nickbi
 * @since 2023-04-07
 */
@Service
@AllArgsConstructor
public class SysNoticeCompanySvcImpl extends ServiceImpl<SysNoticeCompanyMapper, SysNoticeCompany>
    implements ISysNoticeCompanySvc {

  private SysNoticeCompanyMapper sysNoticeCompanyMapper;

  @Override
  public Page<SysNoticeCompanyListVO> selectPage(Page reqPage, SysNoticeCompanyQueryDTO req) {
    LambdaQueryWrapper<SysNoticeCompany> queryWrapper =
        QueryGen.init(new QueryWrapper<SysNoticeCompany>(), req).lambda();
    Page<SysNoticeCompany> page = sysNoticeCompanyMapper.selectPage(reqPage, queryWrapper);
    Page<SysNoticeCompanyListVO> voPage = BeanUtil.copyProperties(page, Page.class);
    voPage.setRecords(BeanUtil.copyToList(page.getRecords(), SysNoticeCompanyListVO.class));
    return voPage;
  }

  @Override
  public List<SysNoticeCompanyListVO> selectList(SysNoticeCompanyQueryDTO req) {
    LambdaQueryWrapper<SysNoticeCompany> queryWrapper =
        QueryGen.init(new QueryWrapper<SysNoticeCompany>(), req).lambda();
    List<SysNoticeCompany> list = sysNoticeCompanyMapper.selectList(queryWrapper);
    if (ObjectUtil.isEmpty(list)) {
      return Lists.newArrayList();
    }
    ISysCropSvc cropSvc = SpringUtil.getBean(ISysCropSvc.class);
    List<SysCrop> comList =
        cropSvc.listByIds(
            list.stream().map(SysNoticeCompany::getComId).collect(Collectors.toList()));
    Map<Long, String> map =
        comList.stream().collect(Collectors.toMap(SysCrop::getId, SysCrop::getName));

    return list.stream()
        .distinct()
        .map(
            item -> {
              SysNoticeCompanyListVO vo =
                  BeanUtil.copyProperties(item, SysNoticeCompanyListVO.class);
              vo.setComName(map.get(vo.getComId()));
              return vo;
            })
        .collect(Collectors.toList());
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void add(SysNoticeCompanyAddDTO req) {
    remove(
        Wrappers.<SysNoticeCompany>lambdaQuery()
            .eq(SysNoticeCompany::getNoticeId, req.getNoticeId()));
    if (StrUtil.isBlank(req.getComIds())) {
      return;
    }
    Set<Long> ids = IdUtil.split(req.getComIds());
    List<SysNoticeCompany> list =
        ids.stream()
            .map(
                id -> {
                  SysNoticeCompany entity = new SysNoticeCompany();
                  entity.setComId(id);
                  entity.setNoticeId(req.getNoticeId());
                  return entity;
                })
            .collect(Collectors.toList());
    saveBatch(list);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void edit(SysNoticeCompanyEditDTO req) {
    SysNoticeCompany entity = BeanUtil.copyProperties(req, SysNoticeCompany.class);
    sysNoticeCompanyMapper.updateById(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void remove(String ids) {
    Set<Long> idList = IdUtil.split(ids);
    this.baseMapper.deleteBatchIds(idList);
  }

  @Override
  public SysNoticeCompanyVO view(Long id) {
    SysNoticeCompany entity = sysNoticeCompanyMapper.selectById(id);
    return BeanUtil.copyProperties(entity, SysNoticeCompanyVO.class);
  }
}
